pimpas.AJAX.MyFacesHax = new pimpas.AJAX.Populate.Listener();
pimpas.Extends(pimpas.AJAX.MyFacesHax,
			   {
					callBack: function(obj, evt, text, xml) {
						switch(evt) {
							case pimpas.AJAX.Populate.events.onPopulateEnded:
								// get jsf_sequence element
								// @deprecated
								/*								
								this.log.info("[MyFaces HAX]: Update jsf_sequence in all forms!");
								var jsf_sequence = xml.eval( new pimpas.XML.Xpath( '//ajax//jsf_sequence' ) ).iterateNext( );
								var newValue = null;
								var root = null;
								if(pimpas.DOM.isDefined( jsf_sequence )) {
									newValue = xml.getAsText( jsf_sequence );
									root = window.document.getElementsByTagName("form");
									for(var i=0;i<root.length;i++)
										this.updateGlobalField( root[i], newValue, "jsf_sequence" );
								}
								*/
								
								// get viewstate element
								this.log.info("[MyFaces HAX]: Update viewState in all forms!");
								var it = xml.eval( new pimpas.XML.Xpath( '//ajax//viewstate' ) );
								var vs = null;
								while((vs=it.iterateNext())!=null) {
									if(pimpas.DOM.isDefined( vs )) {
										newValue = xml.getAsText( vs );
										root = window.document.getElementsByTagName("form");
										var id = xml.getAttributeValue( vs, 'id' );
										for(var i=0;i<root.length;i++)
											this.updateGlobalField( root[i], newValue, id );
									}
								}
							break;
						}
					},
					updateGlobalField: function(el, value, fieldName) {
						if(pimpas.DOM.isDefined(el.name) || pimpas.DOM.isDefined(el.id))
							if(el.name == fieldName || el.id == fieldName) {
								this.log.info("[MyFaces HAX]: Element "+fieldName+" found, update!");
								pimpas.DOM.setObjectValue( el, value );
							}
								
							for(var i=0;i<el.childNodes.length;i++)
								this.updateGlobalField(el.childNodes[i], value, fieldName);
					}
			   }
);

pimpas.AJAX.HandlePopulateError = new pimpas.AJAX.Populate.Listener();
pimpas.Extends(pimpas.AJAX.HandlePopulateError,
			   {
					callBack: function(obj, evt, text, xml) {
						switch(evt) {
							case pimpas.AJAX.Populate.events.onPopulateError:
							break;
						}
					}
			   }
);

pimpas.AJAX.Populate.ProcessAJAX = Class.create( );
pimpas.Extends(pimpas.AJAX.Populate.ProcessAJAX, pimpas.Object);
pimpas.Extends(pimpas.AJAX.Populate.ProcessAJAX,
			   {
				    AJAXMODE: "AJAX_MODE_ENABLED",
					AJAXCALLER: "AJAX_CALLER",
					execute: function(obj, async, immediate, withForm, loading, extraParameters) {
						// create input populate
						obj = pimpas.DOM.getElement( obj );
						var ajaxPopulate = new pimpas.AJAX.Populate( );
						if(pimpas.DOM.isDefined(loading) && pimpas.DOM.getElement(loading) != null)
							ajaxPopulate.addListener( new pimpas.AJAX.Control( loading ) );
						// create request
						var ajaxRequest;
						if(!immediate) {
							HTTPRequestPool.init( );
							ajaxRequest = pimpas.HTTPRequest.getInstance( );
						} else
							ajaxRequest = new pimpas.HTTPRequest( );
						
						var trickRequest = ajaxRequest.request;
						ajaxRequest.request = function() {
							this.setAsync( async );
							// exclude any kind of buttons
							var excludeFilter = new pimpas.Filter( );
							excludeFilter.accept = function(element) {
								return (element == obj) || (element.type != 'button' && element.type != 'submit');
							}
							
							if(withForm == true)
								this.sendForm( obj, excludeFilter );
								
							// put ajax request identifier
							this.addParameter(pimpas.AJAX.Populate.ProcessAJAX.AJAXMODE, "true");
							this.addParameter(pimpas.AJAX.Populate.ProcessAJAX.AJAXCALLER, (obj.id) ? obj.id : obj.name);
							
							// process extra parameters
							if(pimpas.DOM.isDefined(extraParameters))
								for(var i=0;i<extraParameters.length;i++)
									this.addParameter(extraParameters[i].name, encodeURI(extraParameters[i].value));
							
							trickRequest.call(this);
						}
						
						// put the request
						ajaxPopulate.setRequest( ajaxRequest );
						// myfaces hax
						ajaxPopulate.addListener( pimpas.AJAX.MyFacesHax );
						ajaxPopulate.addListener( pimpas.AJAX.HandlePopulateError );
						// execute
						try {
						   ajaxPopulate.execute( );
						} catch(e) {
							
						}
				   }
			   }
);